package com.qkst.website.web.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qkst.website.web.bean.CollectionBean;
import com.qkst.website.web.mapper.CollectionMapper;
import com.qkst.website.web.service.CollectionService;

/**
 * @author lihao
 * @description 收藏service
 * @date 2017-05-11
 * @email ahlxlihao@163.com
 *
 */
@Service("collectionService")
@Transactional
public class CollectionServiceImpl implements CollectionService {
	@Autowired
	private CollectionMapper collectionMapper;
	
	/**
	 * 根据收藏条件查询收藏
	 * @param collection
	 * @return List<CollectionBean>
	 */
	public List<CollectionBean> queryCollectionList(CollectionBean collection){
		//分页计算
		return collectionMapper.selectCollectionList(collection);
	}
	
	/**
	 * 查询收藏总记录数
	 * @param collection
	 * @return 
	 */
	@Override
	public int queryCount(CollectionBean collection) {
		return collectionMapper.selectCount(collection);
	}

	
	/**
	 * 添加收藏夹
	 * @param collection
	 * @return b
	 */
	@Override
	public void save(CollectionBean collection,Map<String, Object> result) {
		collection.setCreateTime(new Date());
		collection.setLastUpdate(new Date());
		int num=collectionMapper.insert(collection);//插入
		result.put("collection",collection);
		if(num>0){//插入成功
			result.put("isSuccess", true);
			result.put("code", "000");
		}else{//插入失败
			result.put("isSuccess", false);
			result.put("code", "500");
		}
	}
	
	/**
	 * 取消收藏
	 * @param collection
	 * @return 
	 */
	@Override
	public void cancel(CollectionBean collection, Map<String, Object> result) {
		if(collection.getCollectId()!=null){
			int num=collectionMapper.deleteByPrimaryKey(collection.getCollectId());
			result.put("collection",collection);
			if(num>0){//取消成功
				result.put("isSuccess", true);
				result.put("code", "000");
			}else{//取消失败
				result.put("isSuccess", false);
				result.put("code", "500");
			}
		}else{
			result.put("isSuccess", false);
			result.put("code", "404");
		}
		
	}
	
}
